/*
 * Copyright (C) 2009 aileron.cc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.anime.controller;

import net.nyapr.anime.logic.SoundPlayer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import wiiusej.values.GForce;
import wiiusej.wiiusejevents.physicalevents.ExpansionEvent;
import wiiusej.wiiusejevents.physicalevents.IREvent;
import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent;
import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent;
import wiiusej.wiiusejevents.utils.WiimoteListener;
import wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.DisconnectionEvent;
import wiiusej.wiiusejevents.wiiuseapievents.GuitarHeroInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.GuitarHeroRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent;
import cc.aileron.anime.ApplicationContext;
import cc.aileron.anime.logic.CameraControl;
import cc.aileron.anime.logic.PreviewMovie;
import cc.aileron.anime.logic.SnapshotAction;

import com.google.inject.Inject;

/**
 * @author Aileron
 * 
 */
public class WiimoteController implements WiimoteListener
{
    @Override
    public void onButtonsEvent(final WiimoteButtonsEvent event)
    {
        /**
         * プレビュー状態
         */
        if (previewMovie.playing())
        {
            if (event.isButtonHomeJustPressed())
            {
                logger.debug("stop pewview");
                previewMovie.stop();
                cameraControl.show();
            }
            return;
        }

        /**
         * 保存
         */
        if (event.isButtonAJustPressed())
        {
            snapshot.call();
            soundManager.playCall();
        }

        /**
         * 削除
         */
        if (event.isButtonBJustPressed())
        {
            snapshot.undo();
            soundManager.playUndo();
        }

        /**
         * プレビュー
         */
        if (event.isButtonHomeJustPressed())
        {
            new Thread(previewMovie).start();
        }
    }

    @Override
    public void onClassicControllerInsertedEvent(final ClassicControllerInsertedEvent arg0)
    {
    }

    @Override
    public void onClassicControllerRemovedEvent(final ClassicControllerRemovedEvent arg0)
    {
    }

    @Override
    public void onDisconnectionEvent(final DisconnectionEvent arg0)
    {
    }

    @Override
    public void onExpansionEvent(final ExpansionEvent event)
    {
    }

    @Override
    public void onGuitarHeroInsertedEvent(final GuitarHeroInsertedEvent arg0)
    {
    }

    @Override
    public void onGuitarHeroRemovedEvent(final GuitarHeroRemovedEvent arg0)
    {
    }

    @Override
    public void onIrEvent(final IREvent arg0)
    {
    }

    @Override
    public void onMotionSensingEvent(final MotionSensingEvent event)
    {
        // でも、そもそも振って何かするのは、誤入力が多くて微妙

        final GForce g = event.getGforce();
        final float x = Math.abs(g.getX());
        final float y = Math.abs(g.getY());
        final float z = Math.abs(g.getZ());

        /*
         * 加速度が4以下の場合は、何もしない
         */
        if (x < 4 && y < 4 || z < 4)
        {
            return;
        }
    }

    @Override
    public void onNunchukInsertedEvent(final NunchukInsertedEvent arg0)
    {
    }

    @Override
    public void onNunchukRemovedEvent(final NunchukRemovedEvent arg0)
    {
    }

    @Override
    public void onStatusEvent(final StatusEvent event)
    {

    }

    /**
     * @param snapshot
     * @param previewMovie
     * @param cameraControl
     * @param soundManager
     * @param context
     */
    @Inject
    public WiimoteController(
        final SnapshotAction snapshot,
        final PreviewMovie previewMovie,
        final CameraControl cameraControl,
        final SoundPlayer soundManager,
        final ApplicationContext context)
    {
        this.snapshot = snapshot;
        this.previewMovie = previewMovie;
        this.cameraControl = cameraControl;
        this.soundManager = soundManager;
    }

    private final CameraControl cameraControl;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final PreviewMovie previewMovie;
    private final SnapshotAction snapshot;
    private final SoundPlayer soundManager;
}
